package com.voxel.sense.infomsg.view.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.utils.SizeUtils;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;
import com.voxel.sense.infomsg.module.firm.details.chars.ResourceChartAdapter;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;


public class SourceCharItemDivider extends RecyclerView.ItemDecoration {

    private Drawable mFormDividerIc;
    private Drawable mDivider;

    public SourceCharItemDivider(int resId, int formDividerIc) {
        mDivider = Factory.app().getResources().getDrawable(resId);
        mFormDividerIc = Factory.app().getResources().getDrawable(formDividerIc);

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
//        drawHorizontalLine(c, parent, state);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        drawHorizontalLine(c, parent, state);
    }


    //画横线, 这里的parent其实是显示在屏幕显示的这部分
    public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            //获取child的索引位置

            int childAdapterPosition = parent.getChildAdapterPosition(child);
            if (childAdapterPosition < 0)
                return;

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            ResourceChartAdapter adapter = (ResourceChartAdapter) parent.getAdapter();
            //获取类型
            MultiItemBean bean = adapter.getItems().get(childAdapterPosition);

            //分割线的顶点
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mFormDividerIc.getIntrinsicHeight();

            switch (bean.getType()) {
                case Constants.DES_TYPE:
                    int childTop = child.getTop() + params.topMargin - mFormDividerIc.getIntrinsicHeight();
                    int i1 = childTop + mFormDividerIc.getIntrinsicHeight();
                    //顶部分割线
                    mFormDividerIc.setBounds(left, childTop, right, child.getTop());
                    //将对象画到画布中
                    mFormDividerIc.draw(c);
                    //底部分割线
                    mFormDividerIc.setBounds(left, top, right, bottom);
                    mFormDividerIc.draw(c);

                    break;
                case Constants.HEAD_TYPE:
                case Constants.NORMAL_TYPE:
                    mFormDividerIc.setBounds(left, top, right, bottom);
                    mFormDividerIc.draw(c);
                    break;
                case Constants.REAL_TABLE_CHAR_TYPE:
                    bottom = top + mDivider.getIntrinsicHeight();
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);
                    break;
            }
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int itemPosition = parent.getChildAdapterPosition(view);
        ResourceChartAdapter adapter = (ResourceChartAdapter) parent.getAdapter();
        MultiItemBean bean = adapter.getItems().get(itemPosition);
        switch (bean.getType()) {
            case Constants.DES_TYPE:
                //顶部增加一个15dp的间距，隔开不同的类型
                outRect.set(mFormDividerIc.getIntrinsicHeight(), SizeUtils.dp2px(15) + SizeUtils.px2dp(1), SizeUtils.px2dp(1), mFormDividerIc.getIntrinsicHeight());
                break;
            case Constants.HEAD_TYPE:
            case Constants.NORMAL_TYPE:
                outRect.set(mFormDividerIc.getIntrinsicHeight(), 0, mFormDividerIc.getIntrinsicHeight(), mFormDividerIc.getIntrinsicHeight());
                break;
            case Constants.REAL_TABLE_CHAR_TYPE:
                outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
                break;
        }

    }
}
